﻿@using CoralNode.Core.Entity.Enums
@using CoralNode.MvcClient.Helper
@using Coralcode.Utils.Basic
@using CoralNode.MvcClient.Models.Block
@model CoralNode.Core.Entity.Domains.Block.BlockDomain
@{
    ViewBag.Title = "Edit";
}
@Html.IncludePage("CoralAdmin/prototype", "_Left", new { current = "block" })
<div id="pbody">
    <div class="extra">
        <div class="pbody_top">
            <span class="ico ico_setting_system"></span><span class="t t_c">
                内容设计&nbsp;&nbsp;/&nbsp;&nbsp;<b class=" t_small f_no">编辑内容块</b>
            </span>
        </div>
        <div id="main">
            <div class="funsearch p_1 pl_2 pr_2 extra_normal border-bottom1">
                <a href="/m/prototype" class="ico ico_back btn f_r mt_0-5"></a>
            </div>
            <form id="spform" action="/m/prototype/editBlock" method="post" enctype="multipart/form-data">
                @if (@ViewBag.Msg != null) {
                    <div class="error_msg">
                        @ViewBag.Msg.Msg
                    </div>
                }
                <div class="pt_1 slideswitch pl_2 pr_2" data-js="slideswitch">
                    <div class="switch_tab">
                        <span class="btn"><i class="ico ico_slideswitch_show"></i></span>主要信息
                    </div>
                    <div class="table_form form_main switch_cnt">
                        <div class="pl_2">
                            @Html.AntiForgeryToken()
                            @Html.ValidationSummary(true)
                            <table>
                                <tbody>
                                <tr>
                                    <th width="80">内容块名称<span class="rq">*</span></th>
                                    <td>
                                        @Html.TextBoxFor(m => m.Title, new {@class = "text w_37 required"})
                                        @Html.HiddenFor(n => n.Id)
                                    </td>
                                </tr>
                                <tr>
                                    <th width="80">内容块代码<span class="rq">*</span></th>
                                    <td>
                                        @Html.TextBoxFor(m => m.BlockKey, new {@class = "text w_37 required"})
                                    </td>
                                </tr>
                                <tr>
                                    <th width="80">分组<span class="rq">*</span></th>
                                    <td>
                                        <div class="select_owner w_12-5 " data-meta="{value:'@Model.GroupId',name:'GroupId', btn:'<span class=\'ico ico_slide_down\'></span>' , clickbtn:'<span class=\'ico ico_slide_up\'></span>'}">
                                            <ul>
                                                @foreach (var item in @ViewBag.GroupList)
                                                {
                                                    if (@item.Id == @ViewBag.GroupId)
                                                    {
                                                        <li data-value="@item.Id">
                                                            @item.Title
                                                        </li>
                                                    }
                                                    else
                                                    {
                                                        <li data-value="@item.Id">
                                                            @item.Title
                                                        </li>
                                                    }
                                                }
                                            </ul>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <th>类型</th>
                                    <td>
                                        <div class="select_owner w_20 " data-meta="{name:'Type',value:'@Model.Type',fun:'selectPrototype', btn:'<span class=\'ico ico_slide_down\'></span>' , clickbtn:'<span class=\'ico ico_slide_up\'></span>'}">
                                            <ul>
                                                @foreach (var item in ViewBag.BlockTypeList)
                                                {
                                                    if (Model.Type == @item.Value)
                                                    {
                                                        <li data-value="@item.Value">
                                                            @item.Key
                                                        </li>
                                                    }
                                                    else
                                                    {
                                                        <li data-value="@item.Value">
                                                            @item.Key
                                                        </li>
                                                    }
                                                }
                                            </ul>
                                        </div>
                                    </td>
                                </tr>
                                @if (Model.Type == SiteEnums.BlcokType.StaticNodeList)
                                {
                                    StaticNodeModel dynamicModel = null;
                                    try
                                    {
                                        dynamicModel = Newtonsoft.Json.JsonConvert.DeserializeObject<StaticNodeModel>(Model.Content) ?? new StaticNodeModel();
                                    }
                                    catch (Exception)
                                    {

                                        dynamicModel = new StaticNodeModel();
                                    }
                                    <tr>
                                        <th width="100"><span class="signmark">*</span>内容类型：</th>
                                        <td>
                                            <div class="js_selectType">

                                                <label class=" mr_2">
                                                    <input type="radio" name="type" class="mt_0-5" value="prototype" @(dynamicModel.PrototypeId > 0 ? "checked=checked" : string.Empty)/>原型
                                                </label>
                                                <label class=" mr_2">
                                                    <input type="radio" name="type" class="mt_0-5" value="category" @(dynamicModel.CategoryId > 0 ? "checked=checked" : string.Empty)/>分类
                                                </label>
                                                <label class=" mr_2">
                                                    <input type="radio" name="type" class="mt_0-5" value="all" @(dynamicModel.PrototypeId <= 0 & dynamicModel.CategoryId<=0? "checked=checked" : string.Empty) />不限制
                                                </label>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr data-type="prototype">
                                        <th width="60"><span class="signmark">*</span>原型：</th>
                                        <td>
                                            <div class="select_owner w_12 mr_1" data-meta="{value:'@dynamicModel.PrototypeId',name:'PrototypeId',btn:'<span class=\'ico ico_slide_down\'></span>' , clickbtn:'<span class=\'ico ico_slide_up\'></span>'}">
                                                <ul>
                                                    <li data-value="0">请选择</li>
                                                    @foreach (var item in @ViewBag.PrototypeList)
                                                    {
                                                        <li data-value="@item.Id">@item.Title</li>
                                                    }
                                                </ul>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr data-type="category">
                                        <th>分类：</th>
                                        <td>
                                            <div class="ztreeContainer">
                                                <input type="text" class="text w_12 required js_ztree_trigger" id="category_title"
                                                       value=""/>
                                                <input type="hidden" name="CategoryId" id="parent_id" value="@dynamicModel.CategoryId"/>
                                                <span><a id="menuBtn" href="#" class="js_ztree_trigger">选择</a></span>
                                                <ul id="parent_list" class="ztree" style="margin-top: 0; width: 180px;"></ul>
                                                <textarea class="js_ztree_data" style="display: none;">@ViewBag.ZtreeCategory</textarea>
                                            </div>
                                        </td>
                                    </tr>
                                }
                                </tbody>
                            </table>
                            @Html.Hidden("Content", Model.Content, new { @class = "js_dynamicNodeContent" })
                        </div>
                    </div>
                </div>
                <div class="submitbox p_2 extra_normal">
                    <input type="submit" class="btn_1" value="提    交" />
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $('.nav_left_style2').find('li').eq(4).addClass('current');
        $(".ztreeContainer").each(function () {
            var $this = $(this),
                $text = $this.find("input[type='text']"),
                $hidden = $this.find("input[type='hidden']"),
                data = $this.find(".js_ztree_data").val(),
                $ztree = $this.find(".ztree");
            $this.find(".js_ztree_data").hide();
            $ztree.zTreeInit({
                trigger: $this.find(".js_ztree_trigger"),
                checkFun: function (treeNode) {
                    var showText = treeNode.name;
                    var tempNode = treeNode;
                    while (tempNode.getParentNode() != null) {
                        tempNode = tempNode.getParentNode();
                        showText = tempNode.name + " > " + showText;
                    }

                    $text.val(showText);

                    $hidden.val(treeNode.id);
                },
                data: data
            });
        });

        var $jsSelectType = $(".js_selectType");
        var trs = $jsSelectType.parents("table").first().find("tr[data-type]");
        trs.hide();
        $jsSelectType.find("input[name='type']").click(function (e) {
            var $this = $(this),
                values = $this.val().split(',');
            trs.hide();
            for (var i = 0; i < values.length; i++) {
                trs.filter("[data-type='" + values[i] + "']").show();
            }
        });
        var checkedItem = $jsSelectType.find("input[name='type']:checked");
        if (checkedItem.length > 0)
            checkedItem.trigger("click");
        else {
            $jsSelectType.find("input[name='type']").first().trigger("click");
        }
        $('form').submit(function (e) {
            if ($(".js_dynamicNodeContent").length > 0) {
                var $inputs = $(".js_dynamicNodeContent").prev().find("input[type='text'],input[type='hidden']");
                $inputs.each(function() {
                    if ($(this).parents("tr").is(":hidden")) {
                        $(this).val("");
                    }
                });
                var tempStr = "{";
                tempStr += '"CategoryId":' + ($inputs.filter("[name='CategoryId']").val() == "" || $inputs.filter("[name='CategoryId']").val() == undefined ? 0 : $inputs.filter("[name='CategoryId']").val()) + ",";
                tempStr += '"PrototypeId":' + ($inputs.filter("[name='PrototypeId']").val() == "" || $inputs.filter("[name='PrototypeId']").val() == undefined ? 0 : $inputs.filter("[name='PrototypeId']").val());
                tempStr += "}";
                $(".js_dynamicNodeContent").val(tempStr);
            }
        });
    });
</script>
